package com.study.servlet;

import com.study.mapper.StudentMapper;
import com.study.pojo.Student;
import com.study.util.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/data")
public class ControllerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
        requset.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type = requset.getParameter("type");
        if("studentList".equals(type)){
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            List<Student> studentList = sqlSession.getMapper(StudentMapper.class).getAllStudent();
            requset.setAttribute("studentList",studentList);
            requset.getRequestDispatcher("studentList.jsp").forward(requset,response);
            SqlSessionUtils.close();
        }
        else if("studentAdd".equals(type)){
            String stuId = requset.getParameter("stuId");
            String stuName = requset.getParameter("stuName");
            String stuClass = requset.getParameter("stuClass");
            String stuSpecial = requset.getParameter("stuSpecial");
            String stuAge = requset.getParameter("stuAge");
            String stuGender = requset.getParameter("stuGender");
            Student stu=new Student();
            stu.setStuId(stuId);
            stu.setStuName(stuName);
            stu.setStuClass(stuClass);
            stu.setStuSpecial(stuSpecial);
            stu.setAge(Integer.parseInt(stuAge));
            stu.setGender(stuGender);
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            int i = sqlSession.getMapper(StudentMapper.class).insertStudent(stu);
            if(i==1){
                sqlSession.commit();
                List<Student> allStudent = sqlSession.getMapper(StudentMapper.class).getAllStudent();
                requset.setAttribute("studentList",allStudent);
                requset.getRequestDispatcher("studentList.jsp").forward(requset,response);
                sqlSession.close();
            }
            else{
                response.getWriter().write("网络错误，请您稍后再试");
            }
        }
        else if("studentUpdate".equals(type)){
            Integer stuId = Integer.parseInt(requset.getParameter("stuId"));
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            Student student = sqlSession.getMapper(StudentMapper.class).getStudentById(stuId);
            requset.setAttribute("student",student);
            requset.getRequestDispatcher("studentUpdate.jsp").forward(requset,response);
        }
        else if("studentUpdateSave".equals(type)){
            String stuId = requset.getParameter("stuId");
            String stuName = requset.getParameter("stuName");
            String stuClass = requset.getParameter("stuClass");
            String stuSpecial = requset.getParameter("stuSpecial");
            int age = Integer.parseInt(requset.getParameter("age"));
            String gender = requset.getParameter("gender");
            Student student=new Student();
            student.setStuId(stuId);
            student.setStuName(stuName);
            student.setStuClass(stuClass);
            student.setStuSpecial(stuSpecial);
            student.setAge(age);
            student.setGender(gender);
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            int i = sqlSession.getMapper(StudentMapper.class).updateStudent(student);
            if(i==1){
                sqlSession.commit();
                List<Student> allStudent = sqlSession.getMapper(StudentMapper.class).getAllStudent();
                requset.setAttribute("studentList",allStudent);
                requset.getRequestDispatcher("studentList.jsp").forward(requset,response);
                sqlSession.close();
            }
            else{
                response.getWriter().write("您的网络不通畅，请稍后再试");
            }
        }
        else if("studentDelete".equals(type)){
            String stuId = requset.getParameter("stuId");
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            int id = Integer.parseInt(stuId);
            int i = sqlSession.getMapper(StudentMapper.class).deleteStudentById(id);
            if(i==1){
                sqlSession.commit();
                List<Student> allStudent = sqlSession.getMapper(StudentMapper.class).getAllStudent();
                requset.setAttribute("studentList",allStudent);
                requset.getRequestDispatcher("studentList.jsp").forward(requset,response);
                sqlSession.close();
            }
            else{
                response.getWriter().write("您的网络不通畅，请稍后再试");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
